<?php

namespace app\api\middleware;

use app\exception\ApiException;
use plugin\admin\app\model\User;
use support\Cache;
use Webman\Http\Request;
use Webman\Http\Response;

class CheckToken implements \Webman\MiddlewareInterface
{
    /**
     * @inheritDoc
     */
    public function process(Request $request, callable $handler): Response
    {
        if (get_user_id()) {
            $user = User::select(['id', 'status'])->where("id", get_user_id())->first();

            if (!$user) {
                throw new ApiException('未登录', 401);
            }

            if ($user['status']) {
                throw new ApiException('该账号已被禁用', 401);
            }
            return $handler($request);
        } else {
            throw new ApiException('未登录', 401);
        }
    }
}